<?php

class Ball {
	private $x;
	private $y;
	private $dx;
	private $vel_y;
	private $cuadro;
	
	private $im0;
	private $im1;

	function __construct() {
		$this->x = 150;
		$this->y = 50;

		$this->dx = 2;
		$this->vel_y = 0;

		$this->cuadro = 0;

		$this->im0 = SDL_LoadBMP('images/pelota0.bmp');
		$this->im1 = SDL_LoadBMP('images/pelota1.bmp');

		$color_transparente = SDL_MapRGB($this->im0['format'], 255, 0, 255);
	
		SDL_SetColorKey($this->im0, SDL_SRCCOLORKEY, $color_transparente);
		SDL_SetColorKey($this->im1, SDL_SRCCOLORKEY, $color_transparente);
	}

	function __destruct() {
		SDL_FreeSurface($this->im0);
		SDL_FreeSurface($this->im1);
	}

	function update() {
		$this->x += $this->dx;
		$this->vel_y += 0.1;

		if($this->y + $this->vel_y >= 176) {
			$this->vel_y -= 0.1;
			$this->vel_y *= -1;
			$this->y = 176;
		} else {
			$this->y += (int)$this->vel_y;
		}

		if($this->x >= 302)
			$this->dx = -2;

		if($this->x < 0)
			$this->dx = 2;
	}

	function draw($screen) {
		$dest = array(
			'x' => $this->x,
			'y' => $this->y,
			'w' => 20,
			'h' => 20
		);

		if($this->cuadro == 1) {
			SDL_BlitSurface($this->im1, null, $screen, $dest);
			$this->cuadro = 0;
		} else {
			SDL_BlitSurface($this->im0, null, $screen, $dest);
			$this->cuadro = 1;
		}
	}
}

